﻿@using System.Data
@{
    ViewBag.Title = "";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
    DataTable table1 = ViewData["role"] as DataTable;
}


<input name="id" id="id" type="hidden" value="0" />
<table style="float: left;font-size: 12px;">
    <tr>
        <td><label>姓名</label></td>
        <td><input type="text" id="name" name="name" style="width: 150px;" /></td>
    </tr>
    <tr>
        <td><label>电话</label></td>
        <td><input type="text" id="tell" name="tell" style="width: 200px;" /></td>
    </tr>
    <tr>
        <td><label>账号</label></td>
        <td><input type="text" id="no" name="no" style="width: 160px;" /></td>
    </tr>
    <tr class="p1">
        <td><label>密码</label></td>
        <td><input type="text" id="pwd" name="pwd" style="width: 100px;" /></td>
    </tr>
    <tr class="p1">
        <td><label>确认密码</label></td>
        <td><input type="text" id="qpwd" name="qpwd" style="width: 100px;" /></td>
    </tr>
    <tr>
        <td><label>系统角色</label></td>
        <td>
            <select id="role">
                <option value="0">请选择系统角色</option>
                @{
                    foreach (DataRow row in table1.Rows)
                    {
                        <option value="@row["Id"]">@row["RoleName"]</option>
                    }
                }
            </select>
        </td>
    </tr>
    <tr>
        <td><label>是否启用</label></td>
        <td><input type="checkbox" id="qy" checked="checked" /></td>
    </tr>
    <tr>
        <td><label>备注</label></td>
        <td><input type="text" id="remark" name="remark" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="botton" style="margin-left: 50px;" onclick="edit()">保存</div>
            <div class="botton" style="margin-left: 50px;" onclick="javascript: history.go(-1);">取消</div>
        </td>
    </tr>
</table>

<script>
    var id = "@ViewData["id"]";
    if (parseFloat(id) > 0) {
        $(".p1").hide();
        $.post("@Url.Action("GetOneData")", { id: id }, function(data) {
            var obj = jQuery.parseJSON(data);
            $("#id").val(obj[0].Id);
            $("#dept").val(obj[0].DeptId);
            $("#no").val(obj[0].UserName);
            $("#role").val(obj[0].RoleId);
            $("#remark").val(obj[0].Remark);
            $("#tell").val(obj[0].Tell);
            $("#name").val(obj[0].Name);
            if (obj[0].AdminId == 0) {
                $("#admin").attr("checked", "checked");
            } else {
                $("#admin").removeAttr("checked");
            }
          
            if (obj[0].IsExit == 0) {
                $("#qy").attr("checked", "checked");
            } else {
                $("#qy").removeAttr("checked");
            }
        });
    }

    function edit() {
        var name = $("#name").val();
        if (name.replace(/\ +/g, "") == "") {
            alert("请输入姓名！");
            return false;
        }
        var no = $("#no").val();
        if (no.replace(/\ +/g, "") == "") {
            alert("请输入账号！");
            return false;
        }
        var tell = $("#tell").val();
        if (no.replace(/\ +/g, "") == "") {
            alert("请输入电话号码！");
            return false;
        }
        var pwd = $("#pwd").val();
        var qpwd = $("#qpwd").val();
        var length = pwd.replace(/\ +/g, "").length;
       
        if (parseFloat(id) ==0) {
            if (pwd.replace(/\ +/g, "") == "") {
                alert("请输入密码！");
                return false;
            } else if (length < 6 || length > 17) {
                alert("密码必须在6到16位之间！");
                return false;
            } else {
                if (qpwd != pwd) {
                    alert("两次输入密码不一致！");
                    return false;
                }
            }
        }
        var remark = $("#remark").val();
        if (remark.replace(/\ +/g, "").length > 100) {
            alert("备注最多只能输入100个字！");
            return false;
        }
        var admin = 0;
        if (!$("#admin").is(":checked")) {
            admin = 1;
        }
       
        var qy = 0;
        if (!$("#qy").is(":checked")) {
            qy = 1;
        }
        var dept = $("#dept").val();
        if (dept == 0) {
            alert("请选择部门！");
            return false;
        }
        var role = $("#role").val();
        if (role == 0) {
            alert("请选择角色！");
            return false;
        }
        $.post("@Url.Action("Edit")", { id: id, remark: remark, name: name, admin: admin, qy: qy, dept: dept, role: role, username: no, tell: tell, password: pwd }, function(row) {
            if (row > 0) {
                alert("操作成功！");
                location.href = "@Url.Action("Index")";
             
            } else if (row == -2) {
                alert("您输入的用户名已存在，请重新输入！");
            } else {
                alert("操作失败！");
            }
           

        });
    }

</script>

